import { Body, Controller, Delete, Get, HttpException, Post, Put,Param , Request, UseGuards,Req, Query, Patch} from '@nestjs/common';
import { ApiTags,ApiOkResponse, ApiOperation, ApiExtension} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { UserService } from './user.service';
import { AddUser, AdminLoginDto, AdminLoginRes, EditUser , QueryAdminUserDto, UserId } from './user.dto';
import { AuthService } from '../../auth/admin.service';
import { IRequest } from 'src/interface/common';
import { AdminUser } from 'src/db/schemas/admin/admin-user.schema';
import { AmdinRoleGuard } from 'src/admin/guard/admin.role.guard';


/**用户模块控制器 */
@ApiTags('后台-用户模块')
@Controller('admin/user')
export class AdminUserController {
	constructor(
		private readonly authService: AuthService,
		private readonly userService: UserService,

	) {}

	/**初始化一个超级管理员 */
	@ApiExtension('x-author','xiaobaozi')
	@ApiOperation({summary:"初始化一个超级管理员",description:"初始化一个超级管理员"})
	@ApiOkResponse({
		description: '初始化一个超级管理员返回结果',
	})
	@Post('/init')
	async init() {
		return await this.userService.init();
	}

	/**登录-jwt功能 */
	@ApiExtension('x-author','xiaobaozi')
	@ApiOperation({summary:"后台登录",description:"后台登录"})
	@ApiOkResponse({
		description: '后台登录返回结果',
		type:AdminLoginRes
	})
	@Post('/login')
	async login(@Body() body:AdminLoginDto) {
		return await this.userService.login(body);
	}

	@ApiExtension('x-author','xiaobaozi')
	@ApiOperation({summary:"获取登录信息",description:"获取登录信息"})
	@Get('/info')
	@UseGuards(AuthGuard('admin-jwt'))
	async getInfo(@Req() req: IRequest) {
		return await this.userService.getInfo(req);
	}

	@ApiOperation({summary:"获取管理员列表",description:"获取管理员列表"})
	@ApiOkResponse({
		type:AddUser,
		isArray:true
	})
	@Get()
	@UseGuards(AuthGuard('admin-jwt'),AmdinRoleGuard(["super"]))
	async getUser(@Query() query:QueryAdminUserDto,@Req() req: IRequest) {
		return this.userService.findUser(query,req);
	}

	@ApiOperation({summary:"新增管理员",description:"新增管理员"})
	@Post()
	@UseGuards(AuthGuard('admin-jwt'),AmdinRoleGuard(["super"]))
	async addUser(@Body() Dto: AddUser){
		return await this.userService.addUser(Dto)
	}

	@ApiOperation({summary:"编辑管理员",description:"编辑管理员"})
	@UseGuards(AuthGuard('admin-jwt'),AmdinRoleGuard(["super"]))
	@Patch(':id')
	async updateUser(@Body() body: EditUser){
		return await this.userService.updateUser(body)
	}

	@ApiOperation({summary:"删除管理员",description:"删除管理员"})
	@UseGuards(AuthGuard('admin-jwt'),AmdinRoleGuard(["super"]))
	@Delete('/:id')
	async deleteUser(@Param() params:UserId){
		return await this.userService.deleteUser(params.id)
	}
}
